package interview.hassubstring;

public class HasSubstring {
	public boolean hasSubstring(String source, String target) throws NullPointerException {
		if (source == null && target == null) {
			return true;
		} else if (source == null) {
			return false;
		} else if (target == null) {
			throw new NullPointerException();
		}

		return source.contains(target);
	}

	public boolean hasSubstring2(String source, String target) throws NullPointerException {
		if (source == null && target == null) {
			return true;
		} else if (source == null) {
			return false;
		} else if (target == null) {
			throw new NullPointerException();
		}

		for (int si = 0; si <= source.length() - target.length(); si++) {
			boolean notFound = false;
			for (int ti = 0; ti < target.length(); ti++) {
				if (source.charAt(si + ti) != target.charAt(ti)) {
					notFound = true;
					break;
				}
			}

			if (!notFound) {
				return true;
			}
		}
		return false;
	}
}
